package cn.dnui_zll731.dnuifood_731zlll.controller;
import android.content.Intent;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.List;
import cn.dnui_zll731.dnuifood_731zlll.Adapter.FoodListAdapter;
import cn.dnui_zll731.dnuifood_731zlll.bean.Result;
import cn.dnui_zll731.dnuifood_731zlll.common.Constants;
import cn.dnui_zll731.dnuifood_731zlll.R;
import cn.dnui_zll731.dnuifood_731zlll.bean.FoodBean;
import cn.dnui_zll731.dnuifood_731zlll.bean.isCollected;

import cn.dnui_zll731.dnuifood_731zlll.listener.RetrofitListener;
import cn.dnui_zll731.dnuifood_731zlll.model.FoodModel;
public class FoodListActivity extends BaseActivity implements RetrofitListener {
    private RecyclerView recyclerView;
    private FoodListAdapter foodListAdapter;
    private String Shop_id,phonenum;
    private ImageButton fanhui;
    private ImageView foodlist_collect;
    private TextView shop_name;
    private FoodModel foodModel;
    private String result,shopname;
    @Override
    protected int initlayout() {
        return R.layout.activity_foodlist;
    }
    @Override
    void initViews() {
        recyclerView=findViewById(R.id.rv_footlist);
        fanhui = findViewById(R.id.shop_fanhui);
        shop_name = findViewById(R.id.shop_name);
        foodlist_collect = findViewById(R.id.shop_sc);
    }

    @Override
    void initEvents() {
        //处理返回和收藏暗流的点击事件
        //返回按钮调用finish()关闭当前界面
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(
                this,
                LinearLayoutManager.VERTICAL,
                false
        );//布局效果---垂直布局
        recyclerView.setLayoutManager(linearLayoutManager);//设置效果
        recyclerView.setItemAnimator(new DefaultItemAnimator());//设置动画效果

        fanhui.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        //收藏按钮调用collect()方法
        foodlist_collect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                collect();
            }
        });
    }

    @Override
    void initData() {
        //通过Intent获取店铺名称
        Intent intent=getIntent();
        Shop_id=intent.getStringExtra("shop_id");
        phonenum=intent.getStringExtra("phonenum");
        shopname = intent.getStringExtra("shop_name");
        shop_name.setText(shopname);
        //获取店铺的菜谱列表,调用getFoodList()方法
        getFoodList();
        //获取店铺的收藏状态,调用iscollected()方法
        iscollected();
    }



    //发送获取电铺的菜谱列表的网络请求
    private void getFoodList(){
        foodModel = new FoodModel();
        foodModel.getFoodByShop(Shop_id,this);
    }
    //发送获取电铺的收藏状态的网络请求
    private void iscollected(){   //-----收藏状态
        foodModel = new FoodModel();
        foodModel.isCollected(getUser_id(),Shop_id,"0",this);
    }
    //发送对店铺的收藏信息进行修改的网络请求
    private void collect(){   //-----收藏
        foodModel = new FoodModel();
        foodModel.userCollectShop(getUser_id(),Shop_id,this);
    }
    @Override
    public void onSuccess(Object object, int flag) {
        switch (flag)
        {
            case Constants.GETFOODBYSHOP:
                //获取店铺的菜单列表
                foodListAdapter = new FoodListAdapter(FoodListActivity.this,(List<FoodBean>)object,R.layout.food_item,phonenum,shopname);
                recyclerView.setAdapter(foodListAdapter);
                break;
            case Constants.ISCOLLECTED:
                //获取店铺的收藏状态
                result = ((isCollected)object).getCollected();
                if (result.equals("0")){
                    foodlist_collect.setImageResource(android.R.drawable.btn_star_big_off);
                }else{
                    foodlist_collect.setImageResource(android.R.drawable.btn_star_big_on);
                }
                break;
            case Constants.USERCOLLECTSHOP:
                //对电铺的收藏信息进行修改
                if (((Result)object).getSuccess().equals("1")){
                    if (result.equals("0")){
                        foodlist_collect.setImageResource(android.R.drawable.btn_star_big_on);
                        showToast("收藏成功");

                        result = "1";//------------注意没有修改数据库
                    }else{
                        foodlist_collect.setImageResource(android.R.drawable.btn_star_big_off);
                        showToast("取消收藏成功");
                        result = "0";//------------注意没有修改数据库
                    }
                }else {
                    showToast("操作失败");
                }
                break;
        }
    }

    @Override
    public void onFalure() {
        showToast("网络连接错误");
    }
}
